package com.news.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.utils.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.aspect.Log;
import com.news.entity.Content;
import com.news.entity.Gather;
import com.news.service.ContentService;
import com.news.utils.StaticHtmlUtils;
import com.utils.HtmlParser;
import com.utils.TmStringUtils;

@Controller
@RequestMapping("/news")
public class NewsController {

	@Value("${staticUrl}")
	private String url;
	
	@Autowired
	ContentService contentService;
	
	@Log("进入新闻管理")
	@RequestMapping("/golist.do")
    public String tolist() {
        return "module/news/list";
    }
	
	@Log("查询新闻管理列表")
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(ServletRequest request, ServletResponse response, Page<Content> page, Content view) {
        Map<String, Object> map = new HashMap<>();
        page.setDescs("id");
        List<Content> list = contentService.getContentList(page, view);
        map.put("records", list);
        map.put("total", page.getTotal());
        return map;
    }

    
    /**
     * 难点：静态化得同一条数据是静态页面是覆盖而不是新增.
     * @param id
     * @param request
     * @param response
     * @return
     */
	@Log("新闻管理静态化")
    @RequestMapping("/staticHtml.do")
    @ResponseBody
    public String staticHtml(Integer id,HttpServletRequest request,HttpServletResponse response) {
    	Content content=contentService.selectById(id);
    	if(content!=null){
			//模板1
			String templatePath = "/content";
			String name = StaticHtmlUtils.staticContent(id,content.getStaticLink(),url,content,templatePath,request,response);
			if(TmStringUtils.isNotEmpty(name)){
				//静态化成功,更改路径
				content.setStaticLink(name);
				content.setUpdateTime(new Date());
				contentService.updateById(content);
				return "success";
			}else{
				return "fail";
			}
		}else{
			return "fail";
		}
    }
	
	@Log("进入新闻爬虫")
	@RequestMapping("/godownload.do")
    public String download() {
        return "module/news/download";
    }
	
	
	@Log("爬虫")
	@RequestMapping(value ="/gather.do", method = RequestMethod.POST)
	@ResponseBody
	public String gather(Gather gather){
		String result=contentService.getHtmlSource(gather);
		if("success".equals(result)) {
			return "success";
		}else {
			return "error";
		}
	}
	
}
